package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.ShProductTj;
import com.ruoyi.system.utils.Base64Util;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.ShProduct;
import com.ruoyi.system.service.IShProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/product")
public class ShProductController extends BaseController
{
    @Autowired
    private IShProductService shProductService;

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("system:product:list")
    @GetMapping("/list")
    public TableDataInfo list(ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        startPage();
        List<ShProduct> list = shProductService.selectShProductList(shProduct);
        return getDataTable(list);
    }

    @GetMapping("/listTj")
    public AjaxResult listTj(ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        List<ShProductTj> list = shProductService.selectProductTjList(shProduct);
        return success(list);
    }

    @GetMapping("/listTjPay")
    public AjaxResult listTjPay(ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        List<ShProductTj> list = shProductService.selectProductTjPayList(shProduct);
        return success(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        List<ShProduct> list = shProductService.selectShProductList(shProduct);
        ExcelUtil<ShProduct> util = new ExcelUtil<ShProduct>(ShProduct.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("system:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shProductService.selectShProductById(id));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            } else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            } else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        return toAjax(shProductService.insertShProduct(shProduct));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShProduct shProduct)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shProduct.setAppId(user.getAppIdm());
            } else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shProduct.setAppId(user.getAppIdf());
            } else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shProduct.setAppId(user.getAppId());
            }
        }
        return toAjax(shProductService.updateShProduct(shProduct));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shProductService.deleteShProductByIds(ids));
    }

    @RequiresPermissions("system:product:remove")
    @Log(title = "商品信息", businessType = BusinessType.GENCODE)
    @GetMapping("/export/{ids}")
    public void export(HttpServletResponse response, @PathVariable Long[] ids) throws IOException
    {
        ShProduct param = new ShProduct();
        SysUser loginUser = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(loginUser.getUserId())) {
            if (Base64Util.hasYwType(loginUser.getYwType(), "1")) {
                param.setAppId(loginUser.getAppIdm());
            }else if (Base64Util.hasYwType(loginUser.getYwType(), "2")) {
                param.setAppId(loginUser.getAppIdf());
            }else if (Base64Util.hasYwType(loginUser.getYwType(), "3")) {
                param.setAppId(loginUser.getAppId());
            }
        }
        List<Long> idList = Arrays.asList(ids);
        param.setIds(idList);
        byte[] data = shProductService.downloadCode(param);
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode("动态", "UTF8")+".zip");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
